import {Component, OnInit} from '@angular/core';
import {ApisService} from "../../shared/services/requests/apis.service";
import {RequestsService} from "../../shared/services/requests/requests.service";


@Component({
  selector: 'app-power-consumption',
  templateUrl: './power-consumption.component.html',
  styleUrls: ['./power-consumption.component.scss']
})
export class PowerConsumptionComponent implements OnInit {

  dataList: any;

  powerDataApi = this.apis.apis.dcPower;

  loading: boolean;

  totalPowerCon: any; // 总能耗

  constructor(private apis: ApisService,
              private req: RequestsService) {
  }

  /**
   * *************************************************************************
   * 请求后台数据
   *
   */
  getPowerData() {

    this.loading = true;
    this.req.getData(this.powerDataApi)
      .subscribe(
        res => {
          console.log('成功获取饼图能耗数据');
          console.log(res);

          /* 能耗值 */
          let server = res.Message.server.usage,
              aircon = res.Message.aircon.usage,
              fan = res.Message.fan.usage,
              pdu = res.Message.pdu.usage,
              others = res.Message.others.usage;

          /* 能耗占比*/
          let serverPercent = res.Message.server.proportion,
              airconPercent = res.Message.aircon.proportion,
              fanPercent = res.Message.fan.proportion,
              pduPercent = res.Message.pdu.proportion,
              othersPercent = res.Message.others.proportion;

          /* 能耗数据 */
          this.dataList = [
            {
              name: 'Server',
              value: server,
              percent: serverPercent,
              color: '#4472c4'
            },
            {
              name: 'Air Condition',
              value: aircon,
              percent: airconPercent,
              color: '#a5a5a5'
            },
            {
              name: 'Fan',
              value: fan,
              percent: fanPercent,
              color: '#ed7d31'
            },
            {
              name: 'PDU',
              value: pdu,
              percent: pduPercent,
              color: '#5b9bd5'
            },
            {
              name: 'Others',
              value: others,
              percent: othersPercent,
              color: '#ffbf00'
            }
          ];
          console.log(this.dataList);
          this.loading = false;

        },
        err => {
          console.log('获取饼图能耗数据失败');
          console.log(err);
          this.loading = false;
        })

  }

  refresh(){
    this.getPowerData();
  }

  ngOnInit() {

    this.getPowerData();

  }

}
